package com.jzr.bedside.presenter.impl

import android.content.Context
import com.jzr.bedside.api.Api
import com.jzr.bedside.base.BasePresenter
import com.jzr.bedside.bean.DeptIntroBean
import com.jzr.bedside.bean.HospitalIntroBean
import com.jzr.bedside.bean.TQuestionBean
import com.jzr.bedside.presenter.contract.HospitaDetailsFragmentContract
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class HospitaDetailsFragmentImpl: BasePresenter<HospitaDetailsFragmentContract.View>(),
    HospitaDetailsFragmentContract.Presenter<HospitaDetailsFragmentContract.View>  {



    override fun getHospitalIntro(context: Context? ,vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getHospitalIntro()
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<HospitalIntroBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: HospitalIntroBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getHospitalIntroSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }

    override fun getDeptIntro(context: Context?, vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getDeptIntro(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DeptIntroBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        if (mView != null) mView?.showError(e.toString())
                    }

                    override fun onNext(t: DeptIntroBean?) {
                        dismissLoadingDialog()
                        if (mView != null && t != null && t.code == 0) {
                            mView?.getDeptIntroSuccess(t)
                        } else {
                            mView?.showError(t?.msg)
                        }
                    }
                })
        )
    }


}